package com.example.admin.utils;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;

import java.io.IOException;
import java.text.DecimalFormat;

public class ObjectSerializer<T> extends JsonSerializer<T> {

    private static final DecimalFormat DECIMAL_FORMAT = new DecimalFormat("#.00");

    public ObjectSerializer() {
    }

    @Override
    public void serialize(T value, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {
        if (value instanceof Number) {
            jsonGenerator.writeString(DECIMAL_FORMAT.format(((Number) value).doubleValue()));
        } else if (value instanceof String) {
            jsonGenerator.writeString((String) value);
        } else {
            jsonGenerator.writeNull();
        }
    }
}
